﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XLinq;


namespace WpfApplicationExperimental
{

    /// <summary>
    /// this will is temp will be enentualy repalced with wpf commands among others 
    /// </summary>
   public  class MainManusCategoryGenerator
    {
       public MainManusCategoryGenerator()
       {

       }

       private static XElement parsedMenu;

       public static IDictionary<string, MenuCommandItem> Commands = new Dictionary<string, MenuCommandItem>(); 

       private static IEnumerable<MenuCommandItem> cont;

       /// <summary>
       /// επιστρεφει τα πεδια του κεντρικου outlook toolbar για καθε κατιγορία
       /// </summary>
       /// <param name="menuName"></param>
       /// <returns></returns>
       public static System .Collections .IEnumerable  GetMainMenuCategoryItems(string menuName)
       {
           return GetMainMenuCategoryItemsT(menuName);
       }
       private static System .Collections . IEnumerable GetMainMenuCategoryItemsT(string menuName)
       {
           XElement menu = ParseMainMenuXML();
           IEnumerable<string> cont = from h in
                                          (from c in menu.Elements("Category")
                                           where (string)c.Attribute("name") == menuName
                                           select c).Elements()
                                            select h.Value;
           System.Collections.IEnumerable submenu = cont.ToList();


           return submenu;
       }

       private static XElement ParseMainMenuXML()
       {
           if (parsedMenu == null)
           {
               //parsedMenu = XElement.Load(@"Resources/MainMenu.xml");
               string xml = WpfApplicationExperimental.Properties.Resources.MainMenu;
               System.IO.StringReader reader = new System.IO.StringReader(xml);
               parsedMenu = XElement.Load(reader);

           }
           return parsedMenu;
       }

       private static IEnumerable<MenuCommandItem> CreateFirstInstance(string menuName)
       {

           XElement menu = ParseMainMenuXML();
           IEnumerable<string> cont = from h in
                                          (from c in menu.Elements("Category")
                                           where (string)c.Attribute("name") == menuName
                                           select c).Elements()
                                      select h.Value;


           List<MenuCommandItem> comands = new List<MenuCommandItem>();

           foreach (string tag in cont)
           {
               comands.Add(new MenuCommandItem(tag));
           }
           return comands;

       }

       public static MenuCommandItem GetCommand(IEnumerable<MenuCommandItem > commands,  string tag)
       {
        //   return (from com in commands where com.Tag == tag select com).Single <MenuCommandItem >();

           return commands.Where<MenuCommandItem>(p => p.Tag == tag).Single<MenuCommandItem >();
       }

       public class MenuCommandItem
       {
           private string tag;

           public string Tag
           {
               get { return tag; }
               set { tag = value; }
           }

           private Action action;

           public Action Action
           {
               get { return action; }
               set { action = value; }
           }

           public MenuCommandItem(string tag)
           { this.tag = tag; }

           public MenuCommandItem(string tag,Action action)
           { this.tag = tag; this.action = action; }

           public override string ToString()
           {return Tag;}

           public void Execute()
           { Action(); }
       }
    }
}
